public class MySingleList {
    static class ListNode {
        public ListNode next;//下一个节点的地址
        public int val;//节点的值域

        public ListNode(int val) {
            this.val = val;
        }
    }

        public ListNode head;//表示当前链表的头节点
        public void creatList(){
            ListNode node1 = new ListNode(12);
            ListNode node2 = new ListNode(23);
            ListNode node3 = new ListNode(34);
            ListNode node4 = new ListNode(45);
            ListNode node5 = new ListNode(56);

            node1.next = node2;
            node2.next = node3;
            node3.next = node4;
            node4.next = node5;

            this.head = node1;

    }

    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size(){

        int count = 0;
        ListNode cur = head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public ListNode findindex(int index){
            ListNode cur = head;
            while (index - 1 == 0){
                cur = cur.next;
                index--;
            }
            return cur;
    }
    /*public void addIndex(int index,int data){
        if (index < 0 || index > size()){
            System.out.println("位置不合法！");
            return;
        }
        ListNode cur = findindex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;


    }*/
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;

    }

    public void addLast(int data){
        ListNode cur = head;
        ListNode node = new ListNode(data);
        if (cur == null){
            head = node;
            return;
        }
        while (cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    public void reserve(){
            ListNode cur = head.next;
            ListNode curNext = cur.next;
            head.next = null;
            while (cur != null){
                curNext = cur.next;
                cur.next = head;
                head = cur;
                cur = curNext;
            }
    }

    public void addIndex(int index,int data){

            ListNode node = new ListNode(data);

            if (index < 0 || index >size()){
                System.out.println("位置不合法！");
                return;
            }
            if (index == 0){
                addFirst(data);
                return;
            }
            if (index == size()){
                addLast(data);
                return;
            }
            ListNode cur = head;
            while (index - 1 != 0){
                cur = cur.next;
                index--;
            }
            node.next = cur.next;
            cur.next = node;

    }

    //找到key的前驱
    public ListNode searchPrev(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    public void remove(int key){
        ListNode cur = searchPrev(key);
        if (head == null){
            return;
        }
        if (head.val == key){
            head = head.next;
            return;
        }
        if (cur == null){
            System.out.println("没有要删除的内容！");
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
}
